package components.contorls.superClass
{
    import com.zn.utils.ClassUtil;
    
    import components.core.UIComponent;
    import components.utils.DestroyUtil;
    import components.utils.EventListenerUtil;
    import components.utils.StringUtil;
    
    import flash.events.Event;
    import flash.geom.Rectangle;


	/**
	 *数字图片 
	 * @author zn
	 * 
	 */	
    public class BaseNumImage extends UIComponent
    {
        private var _num:int = 0;

        private var _resURL:String = "";
		private var _numList:Array;
		
        private var _completeIndex:int;
        public function BaseNumImage()
        {
            super();
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
			
            while (numChildren > 0)
				DestroyUtil.destroyChild(removeChildAt(0));
			
            _completeIndex = 0;
			var image:BaseImage;
            if (!StringUtil.isEmpty(_resURL))
			{
				for (var i:int = 0; i < _numList.length; i++) 
				{
                    image = ClassUtil.getObject("components.contorls.Image") as BaseImage;
                    image.scaleContent = false;
                    image.source = resURL + _numList[i] + ".png";
                    EventListenerUtil.addEventListener(image, Event.COMPLETE, imageLoaderComplete);
					addChild(image);
				}
			}
        }

        private function imageLoaderComplete(event:Event):void
        {
            event.stopImmediatePropagation();
            _completeIndex++;
            if (_completeIndex == _numList.length)
            {
                if (layout.updateChild())
                {
                    myHeight = layout.lastHeight;
                    myWidth = layout.lastWidth;
                }
                else
                {
                    var rect:Rectangle = getRect(this);
                    myWidth = rect.width;
                    myHeight = rect.height;
                }
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }
        protected override function measureSizes():Boolean
        {
            return super.measureSizes();
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/
        [Inspectable(defaultValue = "")]
		public function get resURL():String
		{
			return _resURL;
		}
		
		public function set resURL(value:String):void
		{
			_resURL = value;
			invalidate();
		}
		
        [Inspectable(defaultValue = "0")]
		public function get num():int
		{
			return _num;
		}
		
		public function set num(value:int):void
		{
			_num = value;
			
            _numList = String(num).split("");
            if (num < 0)
				_numList.unshift("sub");
			
			invalidate();
		}

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public override function destroy():void
        {
            super.destroy();
        }
		
		public override function validateNow():void
		{
			super.validateNow();
		}
		
		
		protected override function frameConstructedHandler(event:Event):void
		{
			super.frameConstructedHandler(event);
			
            layoutName = "HLayout";
		}
    }
}